<?php

namespace App\Http\Controllers\Admin\SharePrize;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class PeriodController extends BaseController
{

    /**
     * Note: 创建期数
     * author: wangzefei
     * Date: 2022-02-16 11:44:04
     * param {*}
     * return {*}
     * @api {get} /shareprize/period/create
     */
    public function create(Request $request)
    {
        $param = $request->all();
        unset($param['s']);
        $v = app('validator')->make($param, [
            'period_name' => 'required',
            'start_time' => 'required',
            'end_time' => 'required',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $res = app('model_shareprize_period')->insertGetId($param);

        return Response::sendData($res);
    }

    /**
     * Note: 获取期数列表
     * author: wangzefei
     * Date: 2022-02-16 11:44:04
     * param {*}
     * return {*}
     * @api {get} /shareprize/period/list
     */
    public function list(Request $request)
    {
        $params = $request->all();
        $v = app('validator')->make($params, [
            'page' => 'required|integer|min:1',
            'page_size' => 'required|integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $where = ['deleted_at' => null];
        $list = app('model_shareprize_period')
            ->where($where)
            ->orderBy('id', 'desc')
            ->paginate($params['page_size'], ['*'], '', $params['page']);

        return Response::sendData($list);
    }

    /**
     * Note: 编辑
     * author: wangzefei
     * Date: 2022-02-16 11:44:04
     * param {*}
     * return {*}
     * @api {get} /shareprize/period/edit
     */
    public function edit(Request $request)
    {
        $param = $request->all();

        $v = app('validator')->make($param, [
            'id' => 'required',
            'period_name' => 'required',
            'start_time' => 'required',
            'end_time' => 'required',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $id = $param['id'];
        unset($param['s']);
        unset($param['id']);
        $res = app('model_shareprize_period')->where(['id' => $id])->update($param);
        //更新配置表中的时间值
        app('model_shareprize_config')->where(['month' => $id])->update(['start_time' => $param['start_time'], 'end_time' => $param['end_time']]);
        app('model_shareprize_get_record')->where(['month' => $id])->update(['start_time' => $param['start_time'], 'end_time' => $param['end_time']]);

        return Response::sendData($res);
    }

    /**
     * Note: 删除期数
     * author: wangzefei
     * Date: 2022-02-16 11:44:04
     * param {*}
     * return {*}
     * @api {get} /shareprize/period/delete
     */
    public function delete(Request $request)
    {
        $param = $request->all();
        $v = app('validator')->make($param, [
            'id' => 'required',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $id = $param['id'];

        $res = app('model_shareprize_period')->where(['id' => $id])->update(['deleted_at' => date("Y-m-d H:i:s")]);
        app('model_shareprize_config')->where('month', $id)->update(['deleted_at' => date("Y-m-d H:i:s")]);

        return Response::sendData($res);
    }
}
